<template>
  <div>
    <!-- 值班打印 2018/3/7 11:44 zhangQ -->
    <Row>
      <i-col span="16" offset="1">
        <Form ref="dutyForm" :model="dutyForm" label-position="left" :label-width="120">

          <FormItem label="提煤单号" prop="coalNo">
            <Input type="text" v-model="dutyForm.coalNo"></Input>
          </FormItem>

          <FormItem label="提煤单据类型" prop="coalNo">
            <Input type="text" v-model="dutyForm.coalBillType"></Input>
          </FormItem>

          <FormItem label="送货地址" prop="address">
            <Input type="text" v-model="dutyForm.address"></Input>
          </FormItem>

          <FormItem label="值班员" prop="address">
            <Input type="text" v-model="dutyForm.dutyUser"></Input>
          </FormItem>

          <FormItem label="车队" prop="">
            <Select v-model="dutyForm.fleetId" filterable placeholder="车队" clearable>
              <Option v-for="item in fleetList" :value="item.id" :key="item.id">{{ item.login_name }}</Option>
            </Select>
          </FormItem>

          <FormItem label="司机" prop="">
            <Input type="text" v-model="dutyForm.driverName"></Input>
          </FormItem>

          <FormItem label="车牌号" prop="">
            <Input type="text" v-model="dutyForm.truckPlate"></Input>
          </FormItem>

          <FormItem label="车辆类型" prop="">
            <Select v-model="dutyForm.truckType" filterable placeholder="车辆类型"  clearable>
              <Option v-for="item in truckTypeList" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </FormItem>

        </Form>
      </i-col>
    </Row>
    <div class="xlinkModal-footer">
      <Button type="primary" @click="setCoalNo">确认操作</Button>
      <!--<Button type="primary" @click="printCoalNo">打印</Button>-->
    </div>
  </div>
</template>

<script>
  import {setCoalNo, getOwnFleet, getTruckType} from '@api/backEnd/station'
  import {getTruckTypeList} from '@api/backEnd/fleetControl'

  export default {
    name: 'duty-opera-modal',
    data () {
      let dutyForm = {
        coalNo: this.orderData.coalNo ? this.orderData.coalNo : '',
        address: this.orderData.shippingAddress ? this.orderData.shippingAddress : '',
        coalBillType: this.orderData.coalBillType ? this.orderData.coalBillType : '',
        dutyUser: this.orderData.dutyUser ? this.orderData.dutyUser : '',
        fleetId: this.orderData.fleetId ? this.orderData.fleetId : '',
        driverName: this.orderData.driverName ? this.orderData.driverName : '',
        driverId: this.orderData.driverId ? this.orderData.driverId : '',
        truckPlate: this.orderData.truckPlate ? this.orderData.truckPlate : '',
        truckType: ''
      }
      let truckReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
      let fleetList = []
      let truckTypeList = []
      let datas = {dutyForm, fleetList, truckReg, truckTypeList}
      return datas
    },
    computed: {},
    props: {
      orderData: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    methods: {
      getTruckTypeHas () {
        let param = {
          fleetId: this.orderData.fleetId,
          truckPlate: this.orderData.truckPlate
        }
        getTruckType(param).then(res => {
          if (+res.data.isSuccess === 1) {
            this.dutyForm.truckType = res.data.data.id
          }
        })
      },
      getTruckTypeAll () {
        getTruckTypeList().then(res => {
          if (+res.data.isSuccess === 1) {
            this.truckTypeList = res.data.data
          }
        })
      },
      getAllFleetList () {
        getOwnFleet().then(res => {
          if (res.data.isSuccess === 1) {
            this.fleetList = res.data.data
          }
        })
      },
      setCoalNo () {
        if (!(this.dutyForm.coalNo || this.dutyForm.address || this.dutyForm.dutyUser)) {
          this.$Message.warning('请正确填写')
          return
        }
        if (this.dutyForm.fleetId || this.dutyForm.driverName || this.dutyForm.truckPlate || this.dutyForm.truckType) {
          if (!(this.dutyForm.fleetId && this.dutyForm.driverName && this.dutyForm.truckPlate && this.dutyForm.truckType)) {
            this.$Message.warning('请正确车队司机信息')
            return
          }
        }
        if (this.dutyForm.truckPlate) {
          if (!this.truckReg.test(this.dutyForm.truckPlate)) {
            this.$Message.warning('车牌号不正确')
            return
          }
        }
        let param = {
          coalNo: this.dutyForm.coalNo,
          address: this.dutyForm.address,
          subOrderNo: this.orderData.orderNo,
          coalBillType: this.dutyForm.coalBillType,
          dutyUser: this.dutyForm.dutyUser,
          fleetId: this.dutyForm.fleetId,
          driverName: this.dutyForm.driverName,
          driverId: this.dutyForm.driverId,
          truckPlate: this.dutyForm.truckPlate,
          truckType: this.dutyForm.truckType
        }
        setCoalNo(param).then((res) => {
          if (+res.data.isSuccess === 1) {
            this.$Message.success('发布成功')
            this.$emit('close', this.dutyForm)
          } else {
            this.$Notice.error({
              title: '操作失败',
              desc: res.data.res,
              duration: 2
            })
          }
        })
      },
      printCoalNo (row) {
        // printUtils.print({
        //   titile: '打印提煤单号',
        //   template: '驱蚊器翁'
        // })
        this.$emit('print')
      }
    },
    created () {
    },
    mounted () {
      this.getAllFleetList()
      this.getTruckTypeAll()
      if (this.orderData.truckPlate) {
        this.getTruckTypeHas()
      }
    },
    components: {}
  }
</script>

<style scoped>

</style>
